<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Db;

/**
 * 课管理
 *
 * @icon fa fa-circle-o
 */
class Timetable extends Backend
{

    /**
     * Timetable模型对象
     * @var \app\admin\model\Timetable
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Timetable;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function add()
    {
        $params = $this->request->post('row/a');
        if ($this->request->isPost()) {
            $uin_add['merchant_name'] = $params['merchant_name'];
            $uin_add['course_name'] = $params['course_name'];
            $uin_add['label_name'] = $params['label_name'];
            $uin_add['teacher_name'] = $params['teacher_name'];
            $uin_add['stadium_name'] = $params['stadium_name']??"";
            $uin_add['sum'] = $params['sum'];
            $uin_add['c_time'] = $params['c_time'];
            $uin_add['time'] = strtotime($params['time']);
            $uin_add['createtime'] = time();
            $merchant_data = Db::name("merchant")->where(['name'=>$params['merchant_name']])->find();
            if(!$merchant_data){
                $this->error("商家不存在");
            }

            $course_data = Db::name("course")->where(['name'=>$params['course_name']])->find();
           /* if(!$course_data){
                $this->error("课程不存在");
            }*/
            $stadium_data = Db::name("stadium")->where(['name'=>$params['stadium_name']])->find();
            if(!$stadium_data){
                $this->error("场馆不存在");
            }
            $label_data = Db::name("course_label")->where(['name'=>$params['label_name']])->find();
            $uin_add['merchant_user_id'] =$merchant_data['id'];
            $uin_add['course_id'] =$course_data['id']??0;
            $uin_add['label_id'] =$label_data['id']??0;
            $c_time_arr = explode('-',$uin_add['c_time']);
            $uin_add['c_str_time'] = strtotime(date('Y-m-d '.$c_time_arr[0],$uin_add['time']));
            Db::name("timetable")->insertGetId($uin_add);
            $this->success("添加成功");
        }
        return parent::add();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $params = $this->request->post('row/a');
        if ($this->request->isPost()) {
            $uin_add['merchant_name'] = $params['merchant_name'];
            $uin_add['course_name'] = $params['course_name'];
            $uin_add['label_name'] = $params['label_name'];
            $uin_add['teacher_name'] = $params['teacher_name'];
            $uin_add['stadium_name'] = $params['stadium_name']??"";
            $uin_add['sum'] = $params['sum'];
            $uin_add['c_time'] = $params['c_time'];
            $uin_add['time'] = strtotime($params['time']);
            $uin_add['createtime'] = time();
            $merchant_data = Db::name("merchant")->where(['name'=>$params['merchant_name']])->find();
            if(!$merchant_data){
                $this->error("商家不存在");
            }

            $course_data = Db::name("course")->where(['name'=>$params['course_name']])->find();
            /*if(!$course_data){
                $this->error("课程不存在");
            }*/
            $stadium_data = Db::name("stadium")->where(['name'=>$params['stadium_name']])->find();
            if(!$stadium_data){
                $this->error("场馆不存在");
            }
            $label_data = Db::name("course_label")->where(['name'=>$params['label_name']])->find();

            $uin_add['merchant_user_id'] =$merchant_data['id'];
            $uin_add['course_id'] =$course_data['id']??0;
            $uin_add['label_id'] =$label_data['id']??0;
            $c_time_arr = explode('-',$uin_add['c_time']);
            $uin_add['c_str_time'] = strtotime(date('Y-m-d '.$c_time_arr[0],$uin_add['time']));
             Db::name("timetable")->where(['id'=>$ids])->update($uin_add);
            $this->success("成功");
        }
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        return parent::edit($ids);
    }
    /**
     * 重写import方法
     */
    public function import()
    {
        if($this->request->isPost()){
            $apply_file = $this->request->post('file');
            $objRead = IOFactory::createReader('Xlsx');
            $filePath = ROOT_PATH . DS . 'public' . DS . $apply_file;
            $objSpreadsheet = $objRead->load($filePath);
            $objWorksheet = $objSpreadsheet->getSheet(0);
            $data = $objWorksheet->toArray();
            Db::startTrans();

            foreach ($data as $k=>$v){
                if($k < 1){
                    continue;
                }
                $course_name = $v[0];

                $time = strtotime($v[1]);
                $c_time = $v[2];
                $teacher_name = $v[3];
                $sum = $v[4];
                $merchant_name = $v[5];
                $stadium_name = $v[6];
                $label_name = $v[7]??'';
                $c_time_arr = explode('-',$c_time);
                $ucdata['course_name'] =$course_name;
                $ucdata['label_name'] =$label_name;
                $ucdata['teacher_name'] =$teacher_name;
                $ucdata['time'] =$time+100;
                $ucdata['c_time'] =$c_time;
                $ucdata['sum'] =$sum;
                $ucdata['c_str_time'] = strtotime(date('Y-m-d '.$c_time_arr[0],$ucdata['time']));
                $ucdata['merchant_name'] =$merchant_name;
                $ucdata['stadium_name'] =$stadium_name;

                $ucdata['createtime'] =time();
                $ucdata['updatetime'] =time();
                $merchant_data = Db::name("merchant")->where(['name'=>$merchant_name])->find();
                if(!$merchant_data){
                    Db::rollback();
                    $this->error($merchant_name."商家不存在");
                }
                $teacher_data = Db::name("merchant_teacher")->where(['name'=>$teacher_name])->find();
                if(!$teacher_data){
                    Db::rollback();
                    $this->error($teacher_name."老师不存在");
                }
                $ucdata['teacher_id'] = $teacher_data['id'];
                $ucdata['merchant_user_id'] = $merchant_data['id'];

                $course_data = Db::name("course")->where(['name'=>$course_name])->find();
                /*if(!$course_data){
                    Db::rollback();
                    $this->error($course_name."课程不存在");
                }*/
                $label_data = Db::name("course_label")->where(['name'=>$label_name])->find();
                if(!$label_data){
                   /* Db::rollback();
                    $this->error($course_name."标签不存在");*/
                }
                $stadium_data = Db::name("stadium")->where(['name'=>$stadium_name])->find();
                if(!$stadium_data){
                    Db::rollback();
                    $this->error("场馆不存在");
                }
                $ucdata['course_id'] =$course_data['id']??0;
                $ucdata['label_id'] =$label_data['id']??0;

                //var_dump($ucdata);exit;
                $result =Db::name("timetable")->insertGetId($ucdata);
            }
            Db::commit();
            $this->success('导入成功1');


        }
        // $successMsg[]=' 成功导入'.$count.'条';

        $this->success('导入成功2');

    }
}
